<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['status']=array('0'=>'未显示','1'=>'已显示','2'=>'已驳回');
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 20;
	$params=array('uniacid'=>$_SESSION['uniacid']);
    if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(isset($_RQ['status'])){
		$params['status']=trim($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
    $orderby='createtime desc';
	$total='';
	$return['list']=pdo_getslice('jy_aixiaoyi_wenda',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$v['msg']=cfc('tools')->substr($v['msg'],0,38);
		//$v['title2']=trim($v['title2'],'#');
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$batch=array(
		array('type'=>'post','url'=>'jy_aixiaoyi/wenda/setstatus1/','title'=>'一键生效'),
		array('type'=>'post','url'=>'jy_aixiaoyi/wenda/setstatus0/','title'=>'一键失效'),
		array('type'=>'post','url'=>'jy_aixiaoyi/wenda/set0/','title'=>'一键完全匹配'),
		array('type'=>'post','url'=>'jy_aixiaoyi/wenda/set1/','title'=>'一键包含匹配'),
		array('type'=>'post','url'=>'jy_aixiaoyi/wenda/set3/','title'=>'一键不匹配'),
	);
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>array(
			array('op'=>'list','title'=>'问答库'),
			array('op'=>'edit','title'=>'添加问答'),
			array('op'=>'listadd','title'=>'导入问答'),
			array('op'=>'txt','title'=>'问答txt'),
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：问答优先匹配！【完全匹配】:问题一模一样，才触发；【包含匹配】：完全包含问题就匹配！','list'=>array(
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'title','title'=>'问题','type'=>'string'),
			array('name'=>'title2','title'=>'相同问题','type'=>'string'),
			array('name'=>'msg','title'=>'答案','type'=>'string'),
			array('name'=>'pipei','title'=>'匹配模式','type'=>'radio','options'=>'pipei'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		),
		'batch'=>$batch,
		),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'问题','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'radio','options'=>'status'),
		)
	);
	$return['htmls']=$htmls;
	$return['pipei']=array('完全匹配','包含匹配','随机匹配','不匹配');
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_aixiaoyi_wenda',array('id'=>$id));
		$item['title2']=trim($item['title2'],'#');
	}else{
		$item=array();
	}
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'问答库',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回问答库'),
		),
		'edit'=>array(
			array('name'=>'title','title'=>'问题','type'=>'string'),
			array('name'=>'title2','title'=>'类似问题','type'=>'text','help'=>'用|分开，例如:你是谁？|你是谁|你是？|你谁啊'),
			array('name'=>'msg','title'=>'答案','type'=>'text'),
			array('name'=>'paixu','title'=>'排序','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),		
		)
	);
	$return['htmls']=$htmls;
	exi($return);
	}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$id = $postdata['id'];
	$postdata['latlng']=ijson_encode($postdata['latlng']);
	$data=array_fields(array('msg','status','title','title2','paixu'),$postdata);
	$data['title2']='#'.$data['title2'].'#';
	if(!$data['title']){
		exi('请填写问题','error');
	}
	if(!$data['msg']){
		exi('请填写答案','error');
	}
	if($id){
		pdo_update('jy_aixiaoyi_wenda',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('设置成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_aixiaoyi_wenda',$data);
		exi('添加成功！','','list');
	}
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['status'])){
		$update['status']=intval($post['status']);
	}
	if($update){
		pdo_update('jy_aixiaoyi_wenda',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='setstatus0' || $_RQ['op']=='setstatus1'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择问题','error');
	}
	if($_RQ['op']=='setstatus0'){
		$status=0;
	}elseif($_RQ['op']=='setstatus1'){
		$status=1;
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_aixiaoyi_wenda',array('status'=>$status),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('操作成功！');
}
if($_RQ['op']=='set0' || $_RQ['op']=='set1' || $_RQ['op']=='set3'){
	$ids=trim($_RQ['ids'],';');
	if($ids){
		$ids=explode(';',$ids);
	}else{
		$ids=array();
	}
	if($_RQ['id']){
		$ids[]=$_RQ['id'];
	}
	if(count($ids)==0){
		exi('请先选择问题','error');
	}
	if($_RQ['op']=='set0'){
		$pipei=0;
	}elseif($_RQ['op']=='set1'){
		$pipei=1;
	}elseif($_RQ['op']=='set3'){
		$pipei=3;
	}
	foreach($ids as $id){
		$id=intval($id);
		if($id){
			pdo_update('jy_aixiaoyi_wenda',array('pipei'=>$pipei),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('操作成功！');
}
if($_RQ['op']=='txt'){
	if($_RQ['postdata']){
		$params=array('uniacid'=>$_SESSION['uniacid']);
		if(!empty($_RQ['title'])){
			$params['title like']='%'.trim($_RQ['title']).'%';
			$return['params']['title']=$_RQ['title'];
		}
		if(isset($_RQ['status'])){
			$params['status']=trim($_RQ['status']);
			$return['params']['status']=$_RQ['status'];
		}
		$params['uniacid']=$_SESSION['uniacid'];
		$list=pdo_getall('jy_aixiaoyi_wenda',$params);
		$string='';
		foreach($list as &$v){
			$value='[问]：'.$v['title'].'   [答]：'.$v['msg'];
			$string.=$value."\r\n";
		}
		file_put_contents(ROOT_D.'/addons/jy_aixiaoyi/resource/wenda.txt',$string);
		exi('刷新成功','','/wenda/');
	}
	$return['item']=array('txt'=>SITEROOT.'/addons/jy_aixiaoyi/resource/wenda.txt');
	$htmls=array (
		  'type' => 'edit',
			 'tabs'=>array(
				array('op'=>'list','title'=>'返回问答库'),
			),
		  'edit' => array (
				array ('type' => 'tip','title' => '<red>[温馨提示]：刷新text后，下载text文件，可以永远大模型训练使用！</red>'),
				array ('name' => 'txt','type' => 'copy','title' => 'txt地址','help'=>''),
		  ),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='listadd'){
	if($_RQ['postdata']){
		$postdata=get_postdata();
		$this->model('wenda')->daoru($postdata);
		exi('导入成功','','/wenda/');
	}
	$return['item']=array();
	$htmls=array (
		  'type' => 'edit',
			 'tabs'=>array(
				array('op'=>'list','title'=>'返回问答库'),
			),
		  'edit' => array (
				array ('type' => 'tip','title' => '<red>[温馨提示]：在导入过程中，请不要进行任何操作!请选择本插件导出的问答库表，其他表不支持</red>'),
				array ('name' => 'excel','type' => 'upfile','title' => '上传CSV','help'=>''),
		  ),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
    if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(isset($_RQ['status'])){
		$params['status']=trim($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$params['uniacid']=$_SESSION['uniacid'];
	$list=pdo_getall('jy_aixiaoyi_wenda',$params);
	foreach($list as &$v){
		//$v['title']=base64_encode($v['title']);
		//$v['msg']=base64_encode($v['msg']);
		$v['title']=str_replace(',','，',$v['title']);
		$v['title']=str_replace("\n",' ',$v['title']);
		$v['title']=str_replace("\r",' ',$v['title']);
		$v['msg']=str_replace(',','，',$v['msg']);
		$v['msg']=str_replace("\n",' ',$v['msg']);
		$v['msg']=str_replace("\r",' ',$v['msg']);
	}
	$header=array(
		'title'=>'问题',
		'msg'=>'答案',
	);
	cfc('tools')->export2excel($header,$list,'答案库');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_aixiaoyi_wenda',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_aixiaoyi_wenda',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
?>